package gui;


import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;

import Announce.ThreadNhanThongBao;
import Chat.ChatForClient;
import Chat.ThreadNhanTinHieuChat;


import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JTextField;
import javax.swing.JLabel;

public class ConnectServer extends JFrame {
	private static final long serialVersionUID = 1L;
	
	public JPanel contentPane;
	public JButton btnConnectToServer;
	
	public static ServerInfo si;
	private JTextField txtTen;
	private String ten;
	public ObjectOutputStream oos;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					
					ConnectServer frame = new ConnectServer();
					frame.setVisible(true);
					
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	
	/**
	 * Create the frame.
	 */
	public ConnectServer() {
		setTitle("Client");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(361, 144);
		setLocationRelativeTo(null);
		setResizable(false);
		contentPane = new JPanel();
		setContentPane(contentPane);
		
		btnConnectToServer = new JButton("Connect to Server");
		btnConnectToServer.setBounds(106, 69, 145, 36);
		btnConnectToServer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				KetNoiToiServer();
			}
		});
		contentPane.setLayout(null);
		contentPane.add(btnConnectToServer);
		
		txtTen = new JTextField();
		txtTen.setBounds(146, 21, 104, 20);
		contentPane.add(txtTen);
		txtTen.setColumns(10);
		
		JLabel lblTn = new JLabel("T\u00EAn:");
		lblTn.setBounds(106, 24, 29, 14);
		contentPane.add(lblTn);
		
		si = new ServerInfo();
	}
	
	private void KetNoiToiServer() {
		ten = txtTen.getText().toString();
		// Connect to Observer == Create socketControl
		try {
			//String ipStr = JOptionPane.showInputDialog("Insert Server IP :");
			//int port = Integer.parseInt(JOptionPane.showInputDialog("Insert Server Port :"));
			String ipStr = "127.0.0.1";
			int port = 1234;
			si.controlSocket = new Socket(ipStr, port);
		}catch(NumberFormatException nfe) {
			JOptionPane.showMessageDialog(null, "Wrong integer number format!");
			return;
		} catch (UnknownHostException e) {
			JOptionPane.showMessageDialog(null, "Host not founded!");
			si.controlSocket = null;
			return;
		} catch (IOException e) {
			System.out.println("Failed to create controlSocket!");
			si.controlSocket = null;
			return;
		}		
		System.out.println("Connected to server ... !");
		
		// Send Service Port
		//int portService = 1236;
		int portService = Integer.parseInt(JOptionPane.showInputDialog("Insert Service Port :"));
		try {
			DataOutputStream dos = new DataOutputStream(si.controlSocket.getOutputStream());
			dos.writeInt(portService);
		} catch (IOException e) {
			try {
				si.controlSocket.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			si.controlSocket = null;
			e.printStackTrace();
			return;
		}
		
		// Start Listen Thread
		try {
			si.listenSocket = new ServerSocket(portService);
			
			// Accept service
			
			si.announceSocket = si.listenSocket.accept();			
			si.chatSocket = si.listenSocket.accept();
			
			//si.threadNTHKN = new ThreadNhanTinHieuKetNoi(si);
			//si.threadNTHKN.start();
			
			si.threadNhanTHChat = new ThreadNhanTinHieuChat(si);
			si.threadNhanTHChat.start();
			
			si.threadNhanTB = new ThreadNhanThongBao(si);
			si.threadNhanTB.start();
			
			try {
				oos = new ObjectOutputStream(si.chatSocket.getOutputStream());
			} catch (IOException e) {
				e.printStackTrace();
			}
			//dn;TenMayTinh+ten		(ket noi)
			String s = "dn;" + si.chatSocket.getLocalAddress().getHostName() + "-" + ten;
			try {
				oos.writeUTF(s);
				oos.flush();
				oos.reset();
			} catch (IOException e) {
				e.printStackTrace();
			}

		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, "Cannot open service listen socket!");
			si.listenSocket = null;
			return;
		}
		
		// GUI
		btnConnectToServer.setEnabled(false);
	}
}
